package org.igeek.atomqq.widget;

import org.igeek.atomqq.AtomQQApplication;
import org.igeek.atomqq.R;
import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.domain.Friend;
import org.igeek.atomqq.domain.Group;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.task.FaceTask;
import org.igeek.atomqq.task.GroupMemoTask;
import org.igeek.atomqq.task.FaceTask.FaceResponse;
import org.igeek.atomqq.task.GroupMemoTask.GroupMemoResponse;
import org.igeek.atomqq.task.TaskResultListener;
import org.igeek.atomqq.util.Utils;
import org.igeek.atomqq.vo.Chat;
import org.igeek.atomqq.vo.MsgFriend;
import org.igeek.atomqq.vo.MsgGroup;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/** 
 * 会话
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-5 下午06:43:55 
 */
public class ChatItemView extends LinearLayout {
	private BaseActivity context;
	
	private Chat chat;
	private TextView amtvMarkName;
	private TextView amtvTime;
	private TextView amtvMember;
	private TextView amtvLastMessage;
	
	private ImageView ivFace;
	
	private static long currentUin=0;
	
	
	public ChatItemView(BaseActivity context) {
		super(context);
		
		if(currentUin==0)
			currentUin=((AtomQQApplication)context.getApplication()).getCurrentUser().getUin();
		
		
		
		//填充布局
		LayoutInflater infater=(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		infater.inflate(R.layout.widget_chat_item, this, true);
		
		this.context=context;
		
		amtvMarkName=(TextView) findViewById(R.id.widget_chat_item_amtvMarkname);
		amtvMember=(TextView) findViewById(R.id.widget_chat_item_amtvLastMessageMember);
		amtvTime=(TextView) findViewById(R.id.widget_chat_item_amtvTime);
		amtvLastMessage=(TextView) findViewById(R.id.widget_chat_item_amtvLastMessage);
		ivFace=(ImageView) findViewById(R.id.widget_chat_item_ivFace);
		
	}

	public void setChat(Chat chat) {
		this.chat=chat;
		
		int type=chat.getType();
		if(type==Chat.GROUP){
			MsgGroup group=(MsgGroup) chat.getMessage();
			
			
			String face=Utils.getGroupFacePath(currentUin, group.getGroup_code());
			if("".equals(face))
				ivFace.setImageResource(R.drawable.groupface);
			else
				ivFace.setImageURI(Uri.parse(face));
			
			Group cuGroup=((AtomQQApplication)context.getApplication()).getGroup(group.getGroup_code());
			
			
			
			amtvLastMessage.setText(group.getMessageContents().get(0).getContent());
			String time=String.format("%1$TY-%1$tm-%1$td %1$tT", group.getTime());
			amtvTime.setText(time);
			amtvMember.setText(group.getFrom_uin()+":");
			amtvMarkName.setText(cuGroup.getName());
			amtvMember.setVisibility(View.VISIBLE);
			
		}
		else if(type==Chat.FRIEND){
			MsgFriend friend=(MsgFriend) chat.getMessage();
			
			
			String face=Utils.getFriendFacePath(currentUin, friend.getFrom_uin());
			if("".equals(face))
				ivFace.setImageResource(R.drawable.friendface);
			else
				ivFace.setImageURI(Uri.parse(face));
			
			
			
			
			amtvLastMessage.setText(friend.getMessageContents().get(0).getContent());
			String time=String.format("%1$TY-%1$tm-%1$td %1$tT", friend.getTime());;
			amtvTime.setText(time);
			
			if(0==friend.getFrom_uin()){
				amtvMarkName.setText("我");
			}else{
				Friend cuFriend=((AtomQQApplication)context.getApplication()).getFriend(friend.getFrom_uin());
				amtvMarkName.setText(cuFriend.getNick());
			}
			amtvMember.setVisibility(View.GONE);
		}
		
	}

	public Chat getGroup() {
		return chat;
	}
	


}
